package com.juyu.reader.author.util;

import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class Md5Util {
	/***
	 * MD5加密 生成32位md5码
	 * 
	 * @param 待加密字符串
	 * @return 返回32位md5码
	 */
	public static String md5Encode(String inStr) {
		try {
			MessageDigest md5 = null;

			md5 = MessageDigest.getInstance("MD5");

			byte[] byteArray = inStr.getBytes("UTF-8");
			byte[] md5Bytes = md5.digest(byteArray);
			StringBuffer hexValue = new StringBuffer();
			for (int i = 0; i < md5Bytes.length; i++) {
				int val = ((int) md5Bytes[i]) & 0xff;
				if (val < 16) {
					hexValue.append("0");
				}
				hexValue.append(Integer.toHexString(val));
			}
			return hexValue.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}

	}

	/**
	 * 针对NowPay目前统一的MD5签名方式：key1=value1&key2=value2....keyn=valuen&securityKeySignature
	 * 进行MD5
	 * <p>
	 * 要先对Key进行按字典升序排序
	 * 
	 * @param dataMap
	 *            -- 数据
	 * @param securityKey
	 *            --密钥
	 * @return
	 */
	public static String signNowpayDataMD5(Map<String, String> dataMap, String securityKey) {
		if (dataMap == null) {
			return null;
		}
		Set<String> keySet = dataMap.keySet();
		List<String> keyList = new ArrayList<String>(keySet);
		Collections.sort(keyList);

		StringBuilder toMD5StringBuilder = new StringBuilder();
		for (String key : keyList) {
			String value = dataMap.get(key);

			if (value != null && value.length() > 0) {
				toMD5StringBuilder.append(key + "=" + value + "&");
			}
		}
		String securityKeyMD5 = md5Encode(securityKey);
		toMD5StringBuilder.append(securityKeyMD5);
		String toMD5String = toMD5StringBuilder.toString();
		String lastMD5Result = md5Encode(toMD5String);
		return lastMD5Result;
	}

	public static String signQPayDataMD5(Map<String, String> dataMap, String securityKey) {
		if (dataMap == null) {
			return null;
		}
		Set<String> keySet = dataMap.keySet();
		List<String> keyList = new ArrayList<String>(keySet);
		Collections.sort(keyList);

		StringBuilder toMD5StringBuilder = new StringBuilder();
		for (String key : keyList) {
			String value = dataMap.get(key);

			if (value != null && value.length() > 0) {
				toMD5StringBuilder.append(key + "=" + value + "&");
			}
		}
		toMD5StringBuilder.append("key=" + securityKey);
		String lastMD5Result = md5Encode(toMD5StringBuilder.toString()).toUpperCase();
		return lastMD5Result;
	}

	public static String alipayReadySignContent(Map<String, String> dataMap) {
		if (dataMap == null) {
			return null;
		}
		Set<String> keySet = dataMap.keySet();
		List<String> keyList = new ArrayList<String>(keySet);
		Collections.sort(keyList);

		StringBuilder stringBuilder = new StringBuilder();
		for (String key : keyList) {
			String value = dataMap.get(key);

			if (value != null && value.length() > 0) {
				stringBuilder.append(key + "=" + value + "&");
			}
		}
		if (stringBuilder.length() > 0) {
			stringBuilder.deleteCharAt(stringBuilder.length() - 1);
		}
		return stringBuilder.toString();
	}

	public static String alipayURLEncoderContent(Map<String, String> dataMap, String charset) throws Exception {
		if (dataMap == null) {
			return null;
		}
		Set<String> keySet = dataMap.keySet();
		List<String> keyList = new ArrayList<String>(keySet);
		Collections.sort(keyList);

		StringBuilder stringBuilder = new StringBuilder();
		for (String key : keyList) {
			String value = URLEncoder.encode(dataMap.get(key), charset);

			if (value != null && value.length() > 0) {
				stringBuilder.append(key + "=" + value + "&");
			}
		}
		if (stringBuilder.length() > 0) {
			stringBuilder.deleteCharAt(stringBuilder.length() - 1);
		}
		return stringBuilder.toString();
	}
}
